package com.vanzo.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.vanzo.Tpplication;
import com.vanzo.adapter.ChannelAdapter;
import com.vanzo.bean.Master;
import com.vanzo.encryption.R;

/**
 * Created by tisen on 2018/3/6.
 */

public class ChannelActivity extends BaseActivity implements ChannelAdapter.ChannelSelectListener {

    private RecyclerView recycler;
    private TextView title;
    private TextView lockedTip;
    private ChannelAdapter channelAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_channel);
        title = (TextView) findViewById(R.id.tv_title);
        title.setText("频道设置");
        lockedTip = (TextView) findViewById(R.id.locked_tip);
        lockedTip.setVisibility(tpplication.isLocked() ? View.VISIBLE : View.INVISIBLE);
        recycler = (RecyclerView) findViewById(R.id.channel_recycler);
        channelAdapter = new ChannelAdapter(this);
        recycler.setLayoutManager(new LinearLayoutManager(this));
        recycler.setAdapter(channelAdapter);
        channelAdapter.setChannels(tpplication.getChannels());
        channelAdapter.setSelect(tpplication.getMaster().getChannelId());
        channelAdapter.setListener(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        lockedTip.setVisibility(tpplication.isLocked() ? View.VISIBLE : View.INVISIBLE);
        channelAdapter.setLocked(tpplication.isLocked());
    }

    private void setChannel(Tpplication.Channel channel) {
//        instruction.start();
        instruction.switchChannel(channel.getChannel(), channel.getTfreq(), channel.getRfreq());
    }

    @Override
    public void onChannelSelect(int position) {
        Master master = tpplication.getMaster();
        master.setChannelId(position);
        tpplication.updateMaster(master);
        channelAdapter.setSelect(position);
        setChannel(tpplication.getChannels().get(position));
    }
}
